<?php

/**
 * This is the model class for table "comments".
 * The followings are the available columns in table 'comments':
 * @property integer $comment_id
 * @property interger $post_id
 * @property integer $user_id
 * @property integer $content
 * @property date $time_created
 */
class Comment extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{comments}}';
    }

    public function rules() {
	return array(
	    array('comment_id, post_id, user_id, content, time_created', 'safe')
	);
    }

    public function relations() {
	parent::relations();
	return array(
	    'post' => array(self::BELONGS_TO, 'Posts', 'post_id'),
	    'user' => array(self::BELONGS_TO, 'User', 'user_id')
	);
    }

    //after insert comment to db
    public function afterSave() {
	parent::afterSave();
	//inscrease total comment of post
	$modelPost = Posts::model()->find('post_id=' . $this->post_id);
	$modelPost->total_comment = $modelPost->total_comment + 1;
	$modelPost->save();
    }

}

